package org.cupk2.zhzm.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.cupk2.base.db.CupkDbFields;
import org.cupk2.base.domain.CupkUserContextLoader;
import org.cupk2.base.exception.CupkExceptionUtils;
import org.cupk2.zhzm.dao.ZhzmKnowTagDao;
import org.cupk2.zhzm.domain.ZhzmKnowTag;
import org.cupk2.zhzm.service.ZhzmKnowTagServiceInter;
import org.cupk2.tools.db.DataQuery;
import org.cupk2.tools.db.DataResult;
import org.cupk2.tools.i18n.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**知识标签 
 * @author cbtg自动生成  2025-2-11 15:16:30 
 */
@Service
@Slf4j
public class ZhzmKnowTagServiceImpl implements ZhzmKnowTagServiceInter {


    @Autowired
    private ZhzmKnowTagDao zhzmKnowTagDao;

    @Transactional
    @Override
    public ZhzmKnowTag insertZhzmKnowTagEntity(ZhzmKnowTag zhzmKnowTag) {
        CupkDbFields.initInsertBean(zhzmKnowTag, CupkUserContextLoader.getCurrentUser());

        zhzmKnowTagDao.insert(zhzmKnowTag);


        return zhzmKnowTag;
    }

    @Transactional
    @Override
    public ZhzmKnowTag editZhzmKnowTagEntity(ZhzmKnowTag zhzmKnowTag) {
        ZhzmKnowTag saveZhzmKnowTag = getZhzmKnowTagById(zhzmKnowTag.getId());
        CupkExceptionUtils.throwNullEx(saveZhzmKnowTag, I18n.msg("知识标签不存在:?", zhzmKnowTag.getId()));
        saveZhzmKnowTag.setId(zhzmKnowTag.getId());
        saveZhzmKnowTag.setCtime(zhzmKnowTag.getCtime());
        saveZhzmKnowTag.setCuserkey(zhzmKnowTag.getCuserkey());
        saveZhzmKnowTag.setState(zhzmKnowTag.getState());
        saveZhzmKnowTag.setNote(zhzmKnowTag.getNote());
        saveZhzmKnowTag.setTagkey(zhzmKnowTag.getTagkey());
        saveZhzmKnowTag.setType(zhzmKnowTag.getType());
        saveZhzmKnowTag.setTitle(zhzmKnowTag.getTitle());
        saveZhzmKnowTag.setKnowid(zhzmKnowTag.getKnowid());
         
        CupkDbFields.initUpdateBean(saveZhzmKnowTag, CupkUserContextLoader.getCurrentUser());
        zhzmKnowTagDao.update(saveZhzmKnowTag);
        return saveZhzmKnowTag;
    }

    @Transactional
    @Override
    public ZhzmKnowTag getZhzmKnowTagById(String id) {
        return zhzmKnowTagDao.findById(id);
    }

    @Override
    public List<ZhzmKnowTag> getZhzmKnowTags(DataQuery query) {
        return zhzmKnowTagDao.queryData(query.setCount(false)).getData(ZhzmKnowTag.class);
    }



    @Transactional
    @Override
    public DataResult searchZhzmKnowTag(DataQuery query) {
        DataResult result = zhzmKnowTagDao.queryData(query);
        return result;
    }

    @Override
    public int getZhzmKnowTagNum(DataQuery query) {
        return zhzmKnowTagDao.countData(query);
    }


    @Transactional
    @Override
    public void delZhzmKnowTag(String id) {
        /*[tree：树形结构使用]
        if ( zhzmKnowTagDao.findByParentId(id).size() > 0) {
            throw new RuntimeException("不能删除该节点，请先删除其子节点");
        }
        */
        zhzmKnowTagDao.deleteById(id);
    }
    
    @Override
    public int getNum(DataQuery query) {
        return  zhzmKnowTagDao.countData(query);
    }
    
    /*[tree：树形结构使用]
    @Transactional
    @Override
    public void moveTreeNode(List<String> sourceIds, String targetId) {
        for (String sourceId : sourceIds) {

            ZhzmKnowTag node = getZhzmKnowTagById(sourceId);
            if (!"NONE".equals(targetId)) {
                ZhzmKnowTag target = getZhzmKnowTagById(targetId);
                if (target.getTreecode().indexOf(node.getTreecode()) >= 0) {
                    throw new RuntimeException("不能够移动到其子节点下!");
                }
            }
            node.setParentid(targetId);
            zhzmKnowTagDao.update(node);

            List<ZhzmKnowTag> list = zhzmKnowTagDao.findSubNodes(sourceId);
            for (ZhzmKnowTag treenode : list) {
                initTreeCode(treenode.getId());
            }
        }
    }*/
    
     /**[tree：树形结构使用]
      * 构造treecode字段
     * @param treeNodeId
    private void initTreeCode(String treeNodeId) {
        ZhzmKnowTag node = zhzmKnowTagDao.findById(treeNodeId);
        if (node.getParentid().equals("NONE")) {
            node.setTreecode(node.getId());
        } else {
            node.setTreecode(zhzmKnowTagDao.findById(node.getParentid()).getTreecode() + node.getId());
        }
        zhzmKnowTagDao.update(node);
    }
     */
    /* [tree：树形结构使用]
    @Transactional
    @Override
    public void autoSort(List<String> ids) {
        int sort = 0;
        for (String id : ids) {
            ZhzmKnowTag node =  zhzmKnowTagDao.findById(id);
            if (sort == 0) {
                sort = node.getSortcode();
            }
            node.setSortcode(sort++);
            zhzmKnowTagDao.update(node);
        }
    }*/
}
